package com.sjb.sixteen_journey_backend.utils;

import com.sjb.sixteen_journey_backend.entity.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Date;

// 自定义比较器，根据 postdata 属性进行排序
public class PostDataComparatorUtil implements Comparator<Object> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

    @Override
    public int compare(Object o1, Object o2) {
        LocalDateTime postdate1 = null;
        LocalDateTime postdate2 = null;

        if (o1 instanceof MyTime) {
            postdate1 = ((MyTime) o1).getPostdate();
        } else if (o1 instanceof AirTicket) {
            postdate1 = ((AirTicket) o1).getPostdate();
        } else if (o1 instanceof HotelTicket) {
            postdate1 = ((HotelTicket) o1).getPostdate();
        } else if (o1 instanceof InsuranceTicket) {
            postdate1 = ((InsuranceTicket) o1).getPostdate();
        } else if (o1 instanceof OthersTicket) {
            postdate1 = ((OthersTicket) o1).getPostdate();
        } else if (o1 instanceof ShareThing) {
            postdate1 = ((ShareThing) o1).getPostdate();
        }

        if (o2 instanceof MyTime) {
            postdate2 = ((MyTime) o2).getPostdate();
        } else if (o2 instanceof AirTicket) {
            postdate2 = ((AirTicket) o2).getPostdate();
        } else if (o2 instanceof HotelTicket) {
            postdate2 = ((HotelTicket) o2).getPostdate();
        } else if (o2 instanceof InsuranceTicket) {
            postdate2 = ((InsuranceTicket) o2).getPostdate();
        } else if (o2 instanceof OthersTicket) {
            postdate2 = ((OthersTicket) o2).getPostdate();
        } else if (o2 instanceof ShareThing) {
            postdate2 = ((ShareThing) o2).getPostdate();
        }

        try {
            Date date1 = dateFormat.parse(String.valueOf(postdate1));
            Date date2 = dateFormat.parse(String.valueOf(postdate2));
            return date2.compareTo(date1);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
}